package com.cskaoyan.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.brand.brandVo.regionProvinceVo;
import com.cskaoyan.bean.brand.brandVo.regionProvinceVoVer3;
import com.cskaoyan.bean.domain.Region;
import com.cskaoyan.bean.domain.RegionExample;
import com.cskaoyan.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 行政区域表 服务实现类
 * </p>
 *
 * @author looper
 * @since 2021-11-29
 */
@Service
public class RegionServiceImpl implements RegionService {

    @Resource
    RegionMapper mapper;

    @Override
    public BaseData getRegionList() {
        //先查省的集合 type为1
        List<regionProvinceVo> regionsProvinces = mapper.selectByTypeId(1);
        //市区
        List<regionProvinceVo> regionsCitys = mapper.selectByTypeId(2);
        //县城
        List<regionProvinceVo> regionsTowns = mapper.selectByTypeId(3);
        //遍历添加市区加上每一个县城
        addCity(regionsCitys, regionsTowns);
        //regionsCitys里的市区已经有县城,遍历省会,添加上市区
        addCity(regionsProvinces, regionsCitys);
        int size = regionsProvinces.size();
      return   BaseData.getBack(size,1,1,size,regionsProvinces);
    }

    private void addCity(List<regionProvinceVo> regionsProvinces, List<regionProvinceVo> regionsCitys) {
        for (regionProvinceVo regionsProvince : regionsProvinces) {
            ArrayList cityList = new ArrayList<Region>();
            for (regionProvinceVo regionsCity : regionsCitys) {
                if (regionsProvince.getId().equals(regionsCity.getPid())){
                    cityList.add(regionsCity);
                }
            }
                regionsProvince.setChildren(cityList);
        }
    }
}
